#
# place base class
# alpha version, API and features might change
#
# Copyright (c) 2002 Torvald Riegel. All rights reserved. This program is
# free software; you can redistribute it and/or modify it under the same
# terms as Perl itself.
#



package PetriNet::Coloured::Place;

use strict;
use Carp;

sub new {
    my $type = shift;
    return bless {
                  tokens=> [],
                  net => undef,
                 }, $type;
}

# params: tokens
sub addToken {
    my $self = shift;
    my @id;
    # fills empty places and appends the rest
    foreach (0..$#{$self->{tokens}}) {
        unless (defined $self->{tokens}->[$_]) {
            $self->{tokens}->[$_] = shift;
            push @id, $_;
        }
    }
    my $size = @{$self->{tokens}};
    push @{$self->{tokens}}, @_;
    $self->{net}->tokenAdded($self, @id, $size..($size + @_ - 1))
      if $self->{net};
}


sub getAllTokens {
    my $self = shift;
    return (wantarray ? @{$self->{tokens}} : $self->{tokens});
}

sub getTokens {
    my $self = shift;
    return (wantarray ? $self->{tokens}->[@_] : [$self->{tokens}->[@_]]);
}
sub getToken {
    my $self = shift;
    return $self->{tokens}->[shift];
}


sub removeToken {
    my $self = shift;
    foreach (@_) {
        delete $self->{tokens}->[$_];
    }
    $self->{net}->tokenRemoved($self, @_)
      if $self->{net};
}


sub setNet {
    my $self = shift;
    $self->{net} = shift;
}


# states wether the places themselves produce/consume tokens
# outgoing arcs might add the consumer property, but
# only at the net level.
# FIXME: inconsistent (POE::CPN::Place::Send, cannot be modified at net level,
# it sends all tokens when addToken is called)
sub isProducer {
    return 1;
}

sub isConsumer {
    return 1;
}


1;
